<?php
namespace app\passenger\model;

use app\BaseModel;
use think\facade\Db;

class Coupon extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_passenger_coupon';


    protected $append = [

      'status_text'
    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:状态
     * @author chenniang
     * @DataTime: 2020-03-21 11:53
     */
    public function getStatusTextAttr($value,$data){

        if(!empty($data['status'])&&!empty($data['end_time'])){
            if($data['status']==2){

                $text = '已使用';

            }else{

                $end_time = $data['end_time']-time();
                //转换格式
                $text     =  lbGetDates($end_time);
            }
            return $text;
        }

    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:优惠券记录表
     * @author chenniang
     * @DataTime: 2020-03-09 18:42
     */

    public function AdminList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id')
                ->join('longbing_card_user_info c','a.hx_user = c.fans_id','left')
                ->where($dis)
                ->field('a.*,b.nickName,c.name as hx_name')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){

                if(empty($v['hx_name'])){

                    $v['hx_name'] = Db::name('longbing_card_user')->where(['id'=>$v['hx_user']])->value('nickName');
                }
            }

        }
        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:优惠券详情
     */
    public function couponInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:领取优惠券
     */
    public function couponAdd($data){
        //开始时间
        $data['create_time'] = time();
        //编辑时间
        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:13
     * @return void
     * descrption:编辑优惠券
     */
    public function couponUpdate($dis,$data){

        //编辑时间
        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-17 13:48
     * @功能说明:
     */
    public function couponList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-21 12:03
     * @功能说明:修改状态
     */
    public function changeStatus(){

        $dis[] = ['status','=',1];

        $dis[] = ['end_time','<',time()];

        $res = $this->where($dis)->update(['status'=>3]);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 11:45
     * @功能说明:获取我分享出去的用户
     */
    public function getUserList($coupon){
        $user_dis = [
            //活动id
            'a.active_id' => $coupon['active_id'],
            //邀请人
            'a.share_user'=> $coupon['user_id'],
            //已经使用
//            'a.status'    => 2
        ];
        $data = $this->userCouponList($user_dis);

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 11:48
     * @功能说明:获取被分享的列表
     */
    public function shareUserList($coupon){

        $share_dis = [

            'a.id' => $coupon['id']
        ];

        $data = $this->CouponUserList($share_dis);

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 12:01
     * @功能说明:优惠券码
     */
    public function getCouponQr($coupon,$input,$user_id){
        //获取缓存
        $qr =  getCache($user_id.'-'.$coupon['id']."-coupon_qr",$coupon['uniacid']);
        //如果为空
        if(empty($qr)){
            //码
            $qr_data = [
                //用户id
                'user_id'  => $user_id,
                //优惠券id
                'coupon_id'=> $coupon['id'],
                //活动id
                'active_id'=> $coupon['active_id']
            ];
            //生成码
            $qr = longbingCreateWxCode($coupon['uniacid'],$qr_data,$input['path']);

            $qr = transImagesOne($qr  ,['qr_path'] ,$coupon['uniacid']);
            //写入缓存
            setCache($user_id.'-'.$coupon['id']."-coupon_qr",$qr,3600,$coupon['uniacid']);
        }
        return $qr;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 13:58
     * @功能说明:员工端推荐人记录
     */
    public function staffUserList($staff_id,$active_id){


        $dis = [
          //员工id
          'a.staff_id' => $staff_id,
          //活动id
          'a.active_id'=>$active_id
        ];

        $data = $this->userCouponList($dis);

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-23 11:48
     * @功能说明:获取被分享的列表
     */
    public function CouponUserList($share_dis){

        $data = $this->alias('a')
            ->join('longbing_card_user_info b','a.share_user = b.fans_id','left')
            ->field(['a.coupon_name','a.status','a.type','a.create_time','b.name as user_name','b.avatar as user_img','a.share_user','a.id'])
            ->where($share_dis)
            ->group('a.id')
            ->order('a.id desc')
            ->paginate(10)
            ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //姓名
                $v['user_name'] = !empty($v['user_name'])?$v['user_name']:Db::name('longbing_card_user')->where(['id'=>$v['share_user']])->value('nickName');
                //头像
                $v['user_img']  = !empty($v['user_img'])?$v['user_img']:$this->UserInfoAratar($v['share_user']);
                //友好时间
                $f_time = datachange($v,'create_time');

                $v['create_time'] = $f_time['radar_group'].$f_time['radar_time'];
            }
        }
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-24 12:05
     * @功能说明:
     */

    public function userCouponList($dis){

        $data = $this->alias('a')
            ->join('longbing_card_user_info b','a.user_id = b.fans_id','left')
            ->field(['a.coupon_name','a.type','a.status','a.create_time','b.name as user_name','b.avatar as user_img','a.user_id','a.id'])
            ->where($dis)
            ->group('a.id')
            ->order('a.id desc')
            ->paginate(10)
            ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //姓名
                $v['user_name'] = !empty($v['user_name'])?$v['user_name']:Db::name('longbing_card_user')->where(['id'=>$v['user_id']])->value('nickName');
                //头像
                $v['user_img']  = !empty($v['user_img'])?$v['user_img']:$this->UserInfoAratar($v['user_id']);
                //友好时间
                $f_time = datachange($v,'create_time');

                $v['create_time'] = $f_time['radar_group'].$f_time['radar_time'];

            }
        }
        return $data;
    }


    public function UserInfoAratar($uid){

        $data = Db::name('longbing_card_user')->where(['id'=>$uid])->value('avatarUrl');

        $data = !empty($data)?$data:'https://lbqnyv2.migugu.com/defaultAvatar.png';
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-25 14:08
     * @功能说明:获取名称
     */
    public function getUserName($id){

        $name = Db::name('longbing_card_user_info')->where(['fans_id'=>$id])->value('name');

        if(empty($name)){
            $name = Db::name('longbing_card_user')->where(['id'=>$id])->value('nickName');
        }
        return $name;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-30 10:00
     * @功能说明:获取已经分享的个数
     */
    public function getShareNum($id){


        $count = $this->alias('a')
                ->join('longbing_card_user_info b','a.share_user = b.fans_id AND b.is_staff = 0','left')
                ->where(['active_id'=>$id,'type'=>1])
                ->field('count(a.id) as count,a.share_user')
                ->group(['a.share_user'])
                ->order('count desc')
                ->select();

        if(!empty($count)){

            $count = $count->toArray();

            $count = !empty($count)?$count[0]['count']:0;
        }else{
            $count = 0;
        }

        return $count;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-30 13:18
     * @功能说明:弹幕
     */
    public function couponDm($active_id){

        $dis = [

            'a.type' => 1,

            'a.active_id' => $active_id
        ];

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id')
                ->where($dis)
                ->limit(10)
                ->order('a.id desc')
                ->field('b.avatarUrl,b.nickName')
                ->select()
                ->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['avatarUrl'] = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
            }
        }

        return array_values($data);
    }




}